package com.meiyu.live;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.NetworkError;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.umeng.analytics.MobclickAgent;
import com.meiyu.live.comm.Constant;
import com.meiyu.live.net.LiveRequest;
import com.meiyu.live.net.ReqPackage;
import com.meiyu.live.net.param.BaseBodyParams;
import com.meiyu.live.net.response.BaseResponse;
import com.meiyu.live.util.ToastUtil;
import com.meiyu.live.widget.NetLoadingDailog;

public abstract class BaseFragment extends Fragment implements
        Response.ErrorListener {

    private View contextView;

    private LayoutInflater inflater;

    protected NetLoadingDailog loadingDlg;

    private final DisplayMetrics displayMetrics = new DisplayMetrics();

    @Override
    public final View onCreateView(LayoutInflater inflater,
                                   ViewGroup container, Bundle savedInstanceState) {
        this.inflater = inflater;
        loadingDlg = new NetLoadingDailog(getActivity());
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        contextView = inflater.inflate(getContextView(), null);
        init();
        return contextView;
    }

    public LayoutInflater getInflater() {
        return inflater;
    }

    protected void init() {
    }

    protected View findViewById(int id) {
        return contextView.findViewById(id);
    }

    public abstract int getContextView();

    @Override
    public void onErrorResponse(VolleyError volleyError) {
//        dlgLoad.dismissDialog();
        Log.e("response", volleyError.toString());
        if (volleyError instanceof NetworkError) {
            ToastUtil.show("网络异常，请确认网络通畅后重试");
        } else if (volleyError instanceof TimeoutError) {
            ToastUtil.show("请求数据超时");
        } else {
            ToastUtil.show("服务器异常");
            volleyError.printStackTrace();
        }
        onError();
    }

    public void onError() {
    }

    public <T> void request(BaseBodyParams params, Class<T> response,
                            Response.Listener<? extends BaseResponse> listener) {
        request(Method.POST, params, response, listener);
    }

    public <T> void request(int method, BaseBodyParams params,
                            Class<T> response,
                            Response.Listener<? extends BaseResponse> listener) {
        Response.Listener<T> l = (Response.Listener<T>) listener;
        ReqPackage pack = new ReqPackage(params);
        String paramStr = pack.packetParam();
        LiveRequest<T> liveRequest;

        // if(method == Method.GET) {
        // managerRequest = new RequestManager<T>(method,
        // pack.getUrl() + "?" + paramStr, response, "", l, this);
        // } else {
        Log.i("net-request:" + Constant.URL
                + params.getUrl(), BaseApplication.mGson.toJson(params));
        liveRequest = new LiveRequest<T>(method, Constant.URL
                + params.getUrl(), response, paramStr, l, this);
        // }
        liveRequest.setRetryPolicy(new DefaultRetryPolicy(30000,
                DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

        BaseApplication.getApp().addRequest(liveRequest);

    }

    protected void startActivity(Class<?> clazz) {
        Intent intent = new Intent(getActivity(), clazz);
        startActivity(intent);
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getName());
    }
}
